// pages/index/activity/work/work.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"

const app = getApp()
//获取 log 日志打印
const log = new app.appLogs()
//获取 http 网络请求
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        theme: {},
        pageNumber: 1,
        pageSize: 10,
        tabs: [],
        list: [],
        activityId: null,
        //是否最新  (默认是最新)
        isNew: true,
        //是否人气
        isPopularity: false,
        //当前院落
        yardId: "最新",
        isLoading: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: "活动作品"
        })
        this.setData({
            theme: app.globalData.theme,
            activityId: options.id
            // activityId: "8a80803c73326a760173326a768c0000"
        })
        this.getYardList()
        //默认是获取最新的
        this.getWorkList()
    },

    /**
     * 获取院落列表 （小区列表）
     */
    getYardList: function () {
        http.get('yard/select/area_code',{
            area_code: app.globalData.areaCode,
        }).then(res => {
            if (res.data.code === http.REQUEST_OK) {
                //添加最新和人气项
                let list = [{
                    id: "最新",
                    title: "最新作品"
                },{
                    id: "人气",
                    title: "人气作品"
                },{
                    id: "游客",
                    title: "游客作品"
                }]
                res.data.data.forEach(item => {
                    list.push({
                        id: item.yard_id,
                        title: item.yard_name
                    })
                })
                this.setData({
                    tabs: list
                })
            }else {
                Toast.fail("获取院落列表失败")
            }
        }, err => {
            Toast.fail("网络异常")
        })
    },

    /**
     * 获tab页的作品列表
     * @param yardId 院落id
     */
    getWorkList: function () {
        this.setData({
            isLoading: true
        })
        http.get("activity-work/list", {
            page_number: this.data.pageNumber,
            page_size: this.data.pageSize,
            activity_id: this.data.activityId,//活动id
            yard_id: this.data.yardId //yard_id:为院落id
        }).then(res => {
            if (res.data.code === http.REQUEST_OK) {
                let list = this.data.list
                res.data.data.list.forEach(item => {
                    item.create_time = item.create_time.substr(0,10)
                    list.push(item)
                })
                this.setData({
                    list: list
                })
            }else {
                Toast.fail(res.data.msg)
            }
            this.setData({
                isLoading: false
            })
        }, err => {
            Toast.fail("网络异常")
            this.setData({
                isLoading: false
            })
        })
    },

    /**
     * 当前激活的标签改变时触发
     * @param e
     */
    onTabChange(e) {
        log.e(JSON.stringify(e))
        const index = e.detail.index
        log.e(index)
        this.setData({
            list: [],
            pageNumber: 1
        })
        this.data.yardId = this.data.tabs[index].id
        this.getWorkList()
    },

    /**
     * 点赞 (取消点赞)
     * @param e
     */
    onLike: function (e) {
        const index = e.currentTarget.dataset.index
        const id = e.currentTarget.dataset.id
        log.e(index)
        http.post('activity-like/like',{
            id: id,
            type: 1
        }).then(res => {
            if (res.data.code === http.REQUEST_OK) {
                this.setData({
                    list: [],
                    pageNumber: 1
                })
                this.getWorkList()
            }else {
                Toast.fail("失败")
            }
        }, err => {
            Toast.fail("网络异常")
        })
    },

    /**
     * 上拉加载更多
     * @param e
     */
    onLower: function (e) {
        this.data.pageNumber += 1
        this.getWorkList()
    },

    /**
     * 上传作品
     * @param e
     */
    onAddWork: function (e) {
        wx.navigateTo({
            url: "./add_work/add_work?id=" + this.data.activityId
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})